import Vue from 'vue'
import VueRouter from 'vue-router'
// 注册
Vue.use(VueRouter)

const Layout = () => import('@/layout')

export const asyncRoutes = [
  {
    path: '/',
    name: 'layout',
    component: () => import('@/layout'),
    redirect: '/dashboard',
    meta: {
      title: '首页'
    },
    children: [
      {
        path: '/dashboard',
        name: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: {
          title: 'dashboard', icon: 'dashboard', affix: true
        }
      },
      {
        path: '/dashboard1',
        name: 'dashboard1',
        component: () => import('@/views/dashboard/index'),
        meta: {
          title: 'dashboard1', icon: 'dashboard1', affix: true
        }
      }
    ]
  },
  {
    path: '/test404',
    name: 'layout',
    component: () => import('@/layout'),
    children: [
      {
        path: 'index',
        name: 'notFound',
        component: () => import('@/views/error-page/p404'),
        meta: {
          title: '404', icon: '', affix: true
        }
      }
    ]
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]
export const constantRoutes = [
  // 重定向页面
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    name: 'login',
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/404',
    name: 'page404',
    component: () => import('@/views/error-page/p404'),
    hidden: true
  },
  {
    path: '/401',
    name: 'page401',
    component: () => import('@/views/error-page/p401'),
    hidden: true
  }
]
const createRouter = () => new VueRouter(
  {
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
  }
)

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}
export default router
